<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="3" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- 列筛选-->
    <FilterTable
      ref="table"
      columnsRef="lhmQueryAllMaterial"
      :columns="columns"
      :dataSource="dataSource"
      :isExportExcel="true"
      exportFileName="查询全部前台领料.xlsx"
      :isMemoryPagination="true"
      :mergeCellMethod="mergeCellHandle"
      :isSelectColumn="false"
      :onSyncTableData="getSyncTableData"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectData"
    >
      <!--列操作-->
      <template slot="controls"></template>
    </FilterTable>
  </div>
</template>

<script>
/**
 * 查询全部前台领料
 */
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
import { arrayObjDeWeight } from '@/utils/index';
import dictCode from '@/utils/dictCode';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import { getQueryAllMaterial } from '@/api/repair/queryStatistics/queryAccounts';
import { confirmAction, notifyAction, isFormEmpty } from '@/utils';
// 翻译人员ID
import { getUserListSelect } from '@/api/report/repair/repairreport';
// 翻译车系ID
import { getSeries } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';

export default {
  name: 'LHMQueryAllMaterial',
  components: {},
  mixins: [dictionary, authority],
  data() {
    this.mergeColumns = ['RO_NO'];
    return {
      topFilterList: this.createTopFilterList(),
      dataSource: [],
      columns: this.createTableColumns(),
      mergeArr: [],
      tableData: []
    };
  },
  mounted() {
    // 分页
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.initSelect();
    // this.querySeries();
  },
  methods: {
    async initSelect() {
      // 服务顾问
      let testDrive = { role: '' };
      const res = await searchRoleByCode(testDrive);
      let data = arrayObjDeWeight(res.data, 'userId');
      console.log(res.data, '服务顾问122222222');
      this.columns.find(x => x.dataIndex === 'RECEIVER').filterItems = this.columns.find(x => x.dataIndex === 'RECEIVER').editItems = data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },
    // async querySeries(){
    //   // 车系ID翻译
    //   const car = await getSeries();
    //   console.log(car,'车系ID翻译');
    //   this.columns.find(x => x.dataIndex === 'SERIES').editItems = car.data.map(x => ({ text: x.SERIES_CODE, value: x.SERIES_ID + '' }));
    //   this.columns.find(x => x.dataIndex === 'SERIES_NAME').editItems = car.data.map(x => ({ text: x.SERIES_NAME, value: x.SERIES_ID + '' }));

    // },

    createTopFilterList() {
      return [
        // {
        //   type: 'SELECT',
        //   label: '日期类型',
        //   fieldName: 'dateType',
        //   placeholder: '选择日期',
        //   itemList: [
        //     { text: '出库日期', value: 1001 },
        //     { text: '结算日期', value: 1002 }
        //   ],
        //   // rules: [{ required: true, message: '请输入日期类型', trigger: 'blur' }]
        // },
        {
          type: 'RANGE_DATE',
          label: '出库日期',
          fieldName: 'beginDate|endDate',
          placeholder: '选择日期',
          dateType: 'exactdaterange'
        },
        {
          type: 'RANGE_DATE',
          label: '结算日期',
          fieldName: 'beginTime|endTime',
          placeholder: '选择日期',
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '委托书编号',
          fieldName: 'RO_NO'
        },
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'PART_NO'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '委托书编号',
          dataIndex: 'RO_NO',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '牌照号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        // {
        //   title: '底盘号',
        //   dataIndex: 'VIN',
        //   sorter: true,
        //   filter: true,
        //   showOverflowTooltip: true,
        //   filterType: 'input'
        // },
        {
          title: '备件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '备件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        // {
        //   title: '备件别名',
        //   dataIndex: 'PART_BM',
        //   sorter: true,
        //   filter: true,
        //   showOverflowTooltip: true,
        //   filterType: 'input'
        // },
        {
          title: '出库数量',
          dataIndex: 'PART_QUANTITY',
          sorter: true,
          filter: true,
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        },
        {
          title: '出库单价',
          dataIndex: 'PART_SALES_PRICE',
          sorter: true,
          filter: true,
          precision: 2,
          align: 'right',
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        },
        {
          title: '出库金额',
          dataIndex: 'PART_SALES_AMOUNT',
          sorter: true,
          filter: true,
          precision: 2,
          align: 'right',
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        },
        {
          title: '出库日期',
          dataIndex: 'SEND_TIME',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        // {
        //   title: '领料方式',
        //   dataIndex: 'PICK_TYPE',
        //   sorter: true,
        //   editable: false,
        //   editType: 'select',
        //   editItems: this.createDictList(8111),
        //   filter: true,
        //   showOverflowTooltip: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList(8111)
        // },,
        {
          title: '领料人名称',
          dataIndex: 'RECEIVER',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          editType: 'select',
          editItems: [],
          filterItems: []
        },
        {
          title: '维修状态',
          dataIndex: 'RO_STATUS',
          sorter: true,
          editable: false,
          editType: 'select',
          editItems: this.createDictList('8049'),
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8049')
        },
        {
          title: '结算日期',
          dataIndex: 'BALANCE_TIME',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        // {
        //   title: '车系代码',
        //   dataIndex: 'SERIES',
        //   sorter: true,
        //   filter: true,
        //   showOverflowTooltip: true,
        //   filterType: 'input',
        //   editType: 'select',
        //   editItems: []
        // },
        {
          title: '车系名称',
          dataIndex: 'SERIES_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          editType: 'select',
          editItems: []
        }
        // {
        //   title: '车型代码',
        //   dataIndex: 'MODEL',
        //   sorter: true,
        //   filter: true,
        //   showOverflowTooltip: true,
        //   filterType: 'input',
        //   editType: 'select',
        //   editItems: []
        // },
        // {
        //   title: '车型名称',
        //   dataIndex: 'MODEL_NAME',
        //   sorter: true,
        //   filter: true,
        //   showOverflowTooltip: true,
        //   filterType: 'input',
        //   editType: 'select',
        //   editItems: []
        // },
        // {
        //   title: '领料人代码',
        //   dataIndex: 'RECEIVER_CODE',
        //   sorter: true,
        //   filter: true,
        //   showOverflowTooltip: true,
        //   filterType: 'input',
        //   editType: 'select',
        //   editItems:[]
        // }
      ];
    },
    selectData(val) {
      this.tableData = val;
    },
    async changeHandle(val) {
      this.dataSource = [];
      this.BaseTable.START_LOADING();
      // console.log('val', val);
      // this.printList.beginDate = val.beginDate.slice(0, 10);
      // this.printList.endDate = val.endDate.slice(0, 10);
      if (val.endDate) {
        val.beginDate = val.beginDate.slice(0, 10) + ' 00:00:00';
        val.endDate = val.endDate.slice(0, 10) + ' 23:59:59';
      }
      if (val.endTime) {
        val.beginTime = val.beginTime.slice(0, 10) + ' 00:00:00';
        val.endTime = val.endTime.slice(0, 10) + ' 23:59:59';
      }
      try {
        let res = await getQueryAllMaterial(val);
        if (res.resultCode == 200) {
          this.dataSource = res.data;
        }
      } catch (error) {
      } finally {
        this.BaseTable.STOP_LOADING();
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    // 同步表格数据
    getSyncTableData(list) {
      this.tableData = list;
      this.initHandle(list);
    },
    initHandle(data) {
      console.log(data);
      this.mergeArr = [];
      let pos = 0;
      for (let i = 0; i < data.length; i++) {
        if (i === 0) {
          this.mergeArr.push(1);
          // 第一个被合并单元格行下标
          pos = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].RO_NO === data[i - 1].RO_NO) {
            this.mergeArr[pos] += 1;
            this.mergeArr.push(0);
          } else {
            this.mergeArr.push(1);
            pos = i;
          }
        }
      }
    },
    // 处理合并单元格
    mergeCellHandle({ row, column, rowIndex, columnIndex }) {
      // cloumn列
      // column.propert代表dataIndex常量
      console.log(row, column, rowIndex, columnIndex, 666);
      let isMegre = this.mergeColumns.find(x => x === column.property);
      if (!isFormEmpty(isMegre)) {
        const _row = this.mergeArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
